package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Role;
import com.atguigu.service.PermissionService;
import com.atguigu.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author tangsy
 * @Description TODO
 * @date 2022/9/13 11:46
 * @Version 1.0
 */
@Controller
@RequestMapping("role")
@SuppressWarnings({"unchecked", "rawtypes"})
public class RoleController extends BaseController {

    @Reference
    private RoleService roleService;

    @Reference
    private PermissionService permissionService;

//    @RequestMapping
//    public String index(Model model) {
//        List<Role> list = roleService.findAll();
//
//        model.addAttribute("list", list);
//        return "role/index2";
//    }

    @PreAuthorize("hasAuthority('role.show')")
    @RequestMapping
    public String find(HttpServletRequest request,Model model) {

        Map<String, Object> filters = getFilters(request);
        PageInfo<Role> pageInfo = roleService.findPage(filters);

        model.addAttribute("page",pageInfo);
        model.addAttribute("filters",filters);

        return "role/index";
    }

    @PreAuthorize("hasAuthority('role.create')")
    @RequestMapping("create")
    public String create(){
        return "role/create";
    }

    @PreAuthorize("hasAuthority('role.create')")
    @PostMapping("/save")
    public String save(Role role){

        roleService.insert(role);
        //return "redirect:/role";
        return "common/successPage";

    }

    @PreAuthorize("hasAuthority('role.edit')")
    @GetMapping("/edit/{id}")
    public String edit(Model model,@PathVariable Long id) {
        Role role = roleService.getById(id);
        model.addAttribute("role",role);
        return "role/edit";
    }

    @PreAuthorize("hasAuthority('role.edit')")
    @PostMapping("/update")
    public String update(Role role) {
        roleService.update(role);
        return "common/successPage";
    }

    @PreAuthorize("hasAuthority('role.delete')")
    @GetMapping("delete/{id}")
    public String delete(@PathVariable Long id){
        roleService.delete(id);
        return "redirect:/role";
    }

    @PreAuthorize("hasAuthority('role.assgin')")
    @GetMapping("/assignShow/{roleId}")
    public String assignShow(Model model,@PathVariable Long roleId) {
        List<Map<String,Object>> zNodes = permissionService.findPermissionsByRoleId(roleId);
        model.addAttribute("zNodes", zNodes);
        model.addAttribute("roleId", roleId);
        return "role/assignShow";
    }

    @PreAuthorize("hasAuthority('role.assgin')")
    @PostMapping("/assignPermission")
    public String assignPermission(Long roleId,Long[] permissionIds) {
        permissionService.saveRolePermissionRealtionShip(roleId, permissionIds);
        return "common/successPage";
    }

}
